import router from '@/router'
import setting from './settings'
import userStore from '@/store/modules/user'
// import pinia from './store'
// const userStore = useUserStore(pinia)
// console.log('userStore',userStore)

const whiteList = ['/login', '/404'] // 登录页 404白名单
router.beforeEach(async(to: any, from: any, next: any) => {
  const store = userStore()
  console.log('store',store)
  // document.title = (to.meta.title as string) ?? ''
  document.title = `${setting.title} - ${to.meta.title}`
  if(store.token){
    //登录成功,不能访问login,指向首页
    if (to.path == '/login') {
      next('/')
    } else {
      if (store.userInfoData) {
        next()
      } else {
        try {
          //获取用户信息
          await store.userInfo()
          // console.log('to',to)
          next({ ...to })
        } catch (error) {
          // token过期，退出
          await store.userLogout()
          next('/login')
        }
      }
    }
  } else {
    if (whiteList.includes(to.path)) return next()
    next('/login')
  }
})


router.afterEach((to: any, from: any) => {

})